package org.wlw.service.impl;

import org.wlw.anno.Resource;
import org.wlw.dao.IUserDao;
import org.wlw.utils.PropertiesUtils;

import java.lang.reflect.Method;
import java.util.Locale;
import java.util.Properties;

public class BaseService {
    public BaseService() {
        try {
            Class clz = this.getClass();
            Method[] ms = clz.getDeclaredMethods();
            for(Method m:ms) {
//                System.out.println(m.getName());
                if(m.isAnnotationPresent(Resource.class)) {
                    //需要进行依赖注入
                    Resource res = m.getAnnotation(Resource.class);
                    String tname = res.value();
                    if("".equals(tname)) {
                        //如果val为null，此时需要根据方法的名称来获取值
                        String mname = m.getName();
                        tname = mname.substring(3);
                        String f = tname.substring(0,1);
                        tname = f.toLowerCase()+tname.substring(1);
                    }
                    Properties prop = PropertiesUtils.getPropByFile("obj");
                    String daoName = prop.getProperty(tname);
                    //根据daoName创建对象
                    Class sc = Class.forName(daoName);
                    //创建了dao对象
                    Object obj = sc.newInstance();
                    m.invoke(this,obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
